<template>
  <div class="hero-containe">
    <h1>{{this.id ? '编辑用户': '创建用户'}}</h1>
    <el-form label-width="120px" @submit.native.prevent="save">
      <el-form-item label="账号">
        <el-input v-model="model.name"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="model.password" ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" native-type="submit">保存</el-button>
      </el-form-item>
    </el-form>
    
  </div>
</template>
<script>
export default {
  name: 'AdminUserEdit',
  props: {
    id: String
  },
  data () {
    return {
      model: {
      }
    }
  },
  methods: {
    async save () {
      let result
      
      if(this.id){
       
        result = await this.$http.put(`/rest/admin_users/${this.id}`,this.model)
      } else {
        result = await this.$http.post('/rest/admin_users',this.model)
      }
      
      if(result.data){
        this.$router.push('/admin_users/list')
        this.$message({
          type: 'success',
          message: '保存成功'
        })
      }
    },
    async fetch () {
      const res = await this.$http.get(`/rest/admin_users/${this.id}`)
      this.model = res.data
    }
  },
  created() {
    this.id && this.fetch()
  },
}
</script>
<style scoped>
</style>